package com.example.jiwhsgs;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.DialogInterface;
import android.widget.TextView;

import java.util.Arrays;

public class ZQactivity extends Activity {
    private String[] fate;
    private int[] buttonIds = {R.id.shangshang, R.id.shang, R.id.zhong, R.id.xia, R.id.xiaxia};
    private int selectedButtonId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zq);

        fate = getResources().getStringArray(R.array.fate);
        for (int id : buttonIds) {
            Button button = findViewById(id);
            setButtonClickListener(button, id);
        }

        Button confirmButton = findViewById(R.id.confirm_button);
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectedButtonId == 0) {
                    showAlertDialog("您还没选择命运签！(╬▔皿▔)╯");
                } else {
                    int selectedIndex = getSelectedIndex(selectedButtonId);
                    String selectedFate = fate[selectedIndex];
                    if (fate.length <= 5) {
                        fate = Arrays.copyOf(fate, fate.length + 1);
                    }
                    fate[fate.length - 1] = selectedFate;

                    int randomIndex = (int) (Math.random() * 5);
                    String randomFate = fate[randomIndex];
                    showAlertDialog(randomFate);
                    //fate[0]+'\n'+fate[1]+'\n'+fate[2]+'\n'+fate[3]+'\n'+fate[4]+'\n'+fate[5]
                    TextView textView = findViewById(R.id.confirm_textView);
                    textView.setText(randomFate);
                }
            }
        });
    }

    private void setButtonClickListener(Button button, int buttonId) {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int id : buttonIds) {
                    if (id == buttonId) {
                        button.setBackgroundResource(R.drawable.blue_button);
                        selectedButtonId = buttonId;
                    } else {
                        findViewById(id).setBackgroundResource(R.drawable.zx_button);
                    }
                }
            }
        });
    }

    private void showAlertDialog(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(message);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private int getSelectedIndex(int buttonId) {
        for (int i = 0; i < buttonIds.length; i++) {
            if (buttonIds[i] == buttonId) {
                return i;
            }
        }
        return -1; // 或者抛出异常，根据具体需求
    }
}
